﻿using System;
using System.Linq;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using IFDB.Models.Contexts;
using IFDB.ResourceLibrary;
using IFDB.Models.Foundation;

namespace IFDB.Models.Tables
{
    public class LookupVenue : BaseTable<LookupVenue>
    {
        [Key]
        public Guid LookupVenueId { get; set; }
        public Site Site { get; set; }

        [StringLength(100)]
        public string LookupId { get; set; }
        public Guid VenueId { get; set; }

        [ForeignKey("VenueId")]
        public virtual Venue Venue { get; set; }

        public static LookupVenue CreateNew(Site site, string lookupId, Guid venueId)
        {
            return new LookupVenue()
            {
                LookupVenueId = Guid.NewGuid(),
                Site = site,
                LookupId = lookupId,
                VenueId = venueId
            };
        }

        public static LookupVenue Update(LookupVenue lookupVenue, Site site, string lookupId, Guid venueId)
        {
            lookupVenue.Site = site;
            lookupVenue.LookupId = lookupId;
            lookupVenue.VenueId = venueId;

            return lookupVenue;
        }

        public static LookupVenue Search(IFDBContext db, Site site, string lookupId)
        {
            var query = from lookupVenue in db.LookupVenues
                        where lookupVenue.Site == site
                        && lookupVenue.LookupId == lookupId
                        select lookupVenue;

            var localQuery = from lookupVenue in db.LookupVenues.Local
                             where lookupVenue.Site == site
                             && lookupVenue.LookupId == lookupId
                             select lookupVenue;

            var search = ProcessSearch(localQuery, query, false);
            return search != null ? search.Single() : null;
        }
    }
}
